package net.neiquan.neiquandemowithkf.utils;

import android.app.Activity;

import com.alibaba.sdk.android.oss.model.OSSException;

import net.neiquanlib.inter.AliUpLoadCallBack;

import org.haitao.common.utils.BitmapUtis;

/**
 * 作者 ： hjb
 * 时间 ： 2016/7/23.
 */

public class UploadImage2OSS {

    public static void uploadImage(final Activity context, final String imagePath, final OnUploadImageListener onUploadImageListener) {
        BitmapUtis.compress(imagePath, 600, 800, new BitmapUtis.CompressCallback() {

            @Override
            public void onsucces(String s) {
                AliyunUploadUtils.getInstance(context).uploadPhoto(s, new AliUpLoadCallBack() {
                    @Override
                    public void onSuccess(String RootPath, String bucket, String fileName) {
                        if (onUploadImageListener != null) {
                            onUploadImageListener.onSuccess(RootPath + fileName);
                        }
                    }

                    @Override
                    public void onFailure(String objectKey, OSSException ossException) {
                        if (onUploadImageListener != null) {
                            onUploadImageListener.onFail(ossException.toString());
                        }
                    }
                });
            }

            @Override
            public void onfail() {
                if (onUploadImageListener != null) {
                    onUploadImageListener.onFail("图片压缩失败");
                }
            }
        });
    }

    public interface OnUploadImageListener {
        void onSuccess(String successImg);

        void onFail(String errorMessage);
    }
}
